package cn.com.xy.sms.sdk.ui.popu.util;

import android.content.Context;
import android.view.View.MeasureSpec;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.smsmessage.BusinessSmsMessage;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.util.JsonUtil;
import cn.com.xy.sms.sdk.util.StringUtils;

public class ChannelContentUtil extends ContentUtil {
    private static final String TAG = "ChannelContentUtil";
    public static final String TRAIN_DATE_FORMAT = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_date_format);
    public static final String FIGHT_STATE_DELAYS = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_fight_state_delays);
    public static final String FIGHT_STATE_PLAN = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_fight_state_plan);
    public static final String FIGHT_STATE_DEPARTURE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_fight_state_departure);
    public static final String FIGHT_STATE_RETURN = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_fight_state_return);
    public static final String FIGHT_STATE_ARRIVAL = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_date_arrival);
    public static final String FIGHT_NUMBER_SELECT = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_fight_number_select);
    public static final String TRAIN_SUPPLEMENT_DATE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_dupplement_date);
    public static final String TRAIN_SELECT_SITES = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_ui_part_select_destination);
    public static final String TRAIN_SEAT_SPLIT = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_bubble_train_seat_split);
    public static final String NO_DATA_ARR_TIME = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_double_line_arr_time);
    public static final String NO_DATA_DEP_TIME = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_double_line_dep_time);
    public static final String NO_SEAT_INFO = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_seat_info);
    public static final CharSequence TRAIN_SUPPLEMENT_NUMBER = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_time);
    public static final CharSequence MORE_INFO_SHOW = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_bubble_more_info_show);
    public static final CharSequence MORE_INFO_HIDE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_bubble_more_info_hide);
    public static final int FLIGHT_UNKNOW_TIME_TEXT_SIZE = ContentUtil
            .getDimension(R.dimen.duoqu_air_body_sec_unknow_time_size);
    public static final String FLIGHT_DEF_END = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_fight_end);
    public static final int FLIGHT_TIME_TEXT_SIZE = ContentUtil.getDimension(R.dimen.duoqu_air_body_sec_normal_time_size);
    public static final String FLIGHT_UNKNOW_TIME = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_unknow_time);
    public static final String TRAIN_DEF_END = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_end);
    public static final String FLIGHT_NUMBER_SELECT = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_number_select);
    public static final String TRAIN_NUMBER_SELECT = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_select);
    public static final String TRAIN_DEPART = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_depart);
    public static final String TRAIN_ARRIVE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_arrive);
    public static final String FLIGHT_ARRIVE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_bubble_flight_arrive);
    
    public static final String FLIGHT_STATE_PLAN = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_plan);
    public static final String FLIGHT_STATE_DEPARTURE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_departure);
    public static final String FLIGHT_STATE_RETURN = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_return);
    public static final String FLIGHT_STATE_RETURN_DEPARTURE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_return_departure);
    public static final String FLIGHT_STATE_ALTERNATE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_alternate);
    public static final String FLIGHT_STATE_PLAN_ALTERNATE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_alternate_departure);
    public static final String FLIGHT_STATE_DELAYS = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_delays);
    public static final String FLIGHT_STATE_CANCEL = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_cancel);
    public static final String FLIGHT_STATE_ALTERNATE_CANCEL = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_alternate_cancel);
    public static final String FLIGHT_STATE_RETURN_CANCEL = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_return_cancel);
    public static final String FLIGHT_STATE_AHEAD_CANCEL = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_state_ahead_cancel);
    public static final String FLIGHT_STATE_ARRIVAL = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_date_arrival);
    public static final String FLIGHT_STATE_RETURN_ARRIVAL = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_date_return_arrival);
    public static final String FLIGHT_STATE_ALTERNATE_ARRIVAL = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_flight_date_alternate_arrival);
    public static final String NO_TIME_INFO = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_unknow_time);
    public static final String POST_TITLE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_bubble_post_title);
    public static final String POST_SENDING = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_bubble_post_sending);
    
    public static final int LARGE_CONTENT_SIZE_COUNT = 7;
    public static final int MID_CONTENT_SIZE_COUNT = 14;
    public static final String TABLE_KEY = "duoqu_table_data_horiz";
    
    /* SMARTISAN-261 zhaoxiachao 20161129 start */
    public static final String SEAT_INFO = "duoqu_seat_info";
    public static final String CHANGE_TRAIN = "change_train";
    /* SMARTISAN-261 zhaoxiachao 20161129 end */
    
    public static final String TRAIN_HINT = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_hint);
    public static final String CLICK_FLAG = "click_flag";
    public static final String TRAIN_DATA_QUERY_ERROR = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_data_query_error);
    public static final String TRAIN_DATA_OFF_NETWORK = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_data_off_network);
    public static final String TRAIN_DATA_OVERDUE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_data_overdue);
    public static final String TRAIN_NUMBER_OVERDUE = ContentUtil.getResourceString(Constant.getContext(),
            R.string.duoqu_train_number_overdue);
    
    public static final String BUBBLE_SIZE = "bubble_size";

    public static final int DUOQU_GENERAL_HAS_BOTTOM_PADDBOTTON = (int) Constant.getContext().getResources().getDimension(R.dimen.duoqu_general_sms_padding);
    public static final int DUOQU_POPUPMENU_ARROW_WIDTH = (int) Constant.getContext().getResources().getDimension(R.dimen.duoqu_popmenu_arrow_width);
    public static final int DUOQU_MENU_ICON_WIDTH = (int) Constant.getContext().getResources().getDimension(R.dimen.duoqu_menu_icon_width);

    /**
     * 获取数据依据key
     * 
     * @param data
     * @param jsobj
     * @param key
     * @param type
     * @return
     */
    public static Object getDataByKey(Map<String, String> data, JSONObject jsobj, String key, int type) {
        if (type == 0) {
            // map
            if (data != null) {
                return data.get(key);
            }
        } else {
            // json
            if (jsobj != null && jsobj.has(key)) {
                try {
                    return jsobj.get(key);
                } catch (Throwable e) {
                    SmartSmsSdkUtil.smartSdkExceptionLog(TAG, e);
                }
            }
        }
        return "";
    }

    public static String getContentInfo(boolean isWithBlank, String... obj) {
        StringBuffer sb = new StringBuffer();
        if (obj == null || obj.length <= 0)
            return null;
        for (int i = 0; i < obj.length; i++) {
            String item = obj[i];
            if (item != null) {
                item = item.replaceAll(Constant.Delimiter, "").replaceAll("NULL", "");
            }
            if (!StringUtils.isNull(item)) {
                sb.append(item);
                if (isWithBlank) {
                    sb.append(" ");
                }

            }

        }
        return sb.toString();

    }

    public static String getContentInfo(String orgString, String oldStr, String newStr) {
        try {
            orgString = orgString.replaceAll(oldStr, newStr);
            return orgString;

        } catch (Throwable e) {
            SmartSmsSdkUtil.smartSdkExceptionLog(TAG, e);
        }
        return "";
    }
    
    public static String getFightStateColor(String mState,BusinessSmsMessage smsMessage) {
        if (StringUtils.isNull(mState)) {
            return null;
        }
        String clr1 = null;
        String clr2 = null;
        String clr3 = null;
        if(smsMessage != null){
            clr1 = (String) smsMessage.getValue("v_hd_text_2");
            clr2 = (String) smsMessage.getValue("v_hd_text_3");
            clr3 = (String) smsMessage.getValue("v_hd_text_4");
        }
        
        if(FLIGHT_STATE_CANCEL.equals(mState)
            ||FLIGHT_STATE_DEPARTURE.equals(mState)
            ||FLIGHT_STATE_RETURN.equals(mState)
            ||FLIGHT_STATE_RETURN_DEPARTURE.equals(mState)
            ||FLIGHT_STATE_ALTERNATE.equals(mState)
            ||FLIGHT_STATE_PLAN_ALTERNATE.equals(mState)){
            if(!StringUtils.isNull(clr1)){
                return clr1;
            }else{
                return "3010";
            }
        }else if(FLIGHT_STATE_DELAYS.equals(mState)
                || FLIGHT_STATE_CANCEL.equals(mState)
                || FLIGHT_STATE_ALTERNATE_CANCEL.equals(mState)
                || FLIGHT_STATE_RETURN_CANCEL.equals(mState)
                || FLIGHT_STATE_AHEAD_CANCEL.equals(mState)){
            if(!StringUtils.isNull(clr1)){
                return clr2;
            }else{
                return "1010";
            }
        }else if(FLIGHT_STATE_ARRIVAL.equals(mState)
                || FLIGHT_STATE_RETURN_ARRIVAL.equals(mState)
                || FLIGHT_STATE_ALTERNATE_ARRIVAL.equals(mState)){
            if(!StringUtils.isNull(clr3)){
                return clr3;
            }else{
                return "5010";
            }
        }
        
        return "3010";
    }
    
    

    public static int getWordCount(String s) {
        if (StringUtils.isNull(s)) {
            return 0;
        }
        s = s.replaceAll("[^\\x00-\\xff]", "**");
        int length = s.length();
        return length;
    }
    


    public static final String KEY_BOTTOM_BOTTON = "duoqu_bottom_button";
    public static final String KEY_VALUE_HAS_BOTTOM_BOTTON = "true";
    public static final String KEY_VALUE_NO_BOTTOM_BOTTON = "false";
    /**
     * 
     * @param message
     * @return  true : 有按钮数据 。  false：  无按钮数据
     */
    public static boolean hasBottomBotton(BusinessSmsMessage message){
        if(message == null){
            return true;
        }
        
        Context c = Constant.getContext();
        String hasBotton = (String) message.getValue(ChannelContentUtil.KEY_BOTTOM_BOTTON);
        if(StringUtils.isNull(hasBotton)){
            String groupValue = (String) message.getValue("GROUP_KEY");
            if(StringUtils.isNull(groupValue)){
                groupValue = "";
            }
            HashMap<String, Object> formatBubbleParamMap = message.extendParamMap;
            formatBubbleParamMap.put("isUseNewAction", "true");
            JSONArray actionArr = BottomButtonUtil.getActionArrayData(c,message, groupValue, 2, formatBubbleParamMap);
            if(actionArr==null || actionArr.length()==0){
                hasBotton = ChannelContentUtil.KEY_VALUE_NO_BOTTOM_BOTTON;
            }else{
                hasBotton = ChannelContentUtil.KEY_VALUE_HAS_BOTTOM_BOTTON;
            }
            message.putValue(ChannelContentUtil.KEY_BOTTOM_BOTTON, hasBotton);
        }
        
        if(ChannelContentUtil.KEY_VALUE_HAS_BOTTOM_BOTTON.equals(hasBotton)){
            return true;
        }else{
            return false;
        }
    }
    
    public static int getMaxWidth(BusinessSmsMessage message,TextView textView){
        if(message == null || textView == null){
            return 0;
        }
        int size = message.getTableDataSize(TABLE_KEY);
        int maxWidth=0;
        if(size!=0){
            for(int i=0;i<size;i++){
                JSONObject jsobj = (JSONObject) message.getTableData(i, TABLE_KEY);
                String titleText = (String) JsonUtil.getValFromJsonObject(jsobj, "t1");
                textView.setText(titleText.trim());
                int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
                textView.measure(spec, spec);
                int tvWidth = textView.getMeasuredWidth();
                if(tvWidth > maxWidth){
                    maxWidth = tvWidth;
                }
            }
        }
        return maxWidth;
    }

    
    /* SMARTISAN-280 zhaoxiachao 20161202 start */
    /**
     * 根据传入的字符串进行正则匹配， 拿出字符个数。
     * 字体大小根据字符个数进行适配的缩小或放大。
     * 
     * @param textContent  城市的字符
     * @return 26：  代表不做缩放处理,默认字体大小;  缩放的dp数值
     */
    public static float needUpdateContTextSize(String textContent) {
        if(StringUtils.isNull(textContent)){
            return 32;
        }
        int length = ChannelContentUtil.getWordCount(textContent) / 2;
        if(length <= 7){
            return 32;
        }
        
        return 20;
    }

    public static float needEnglishUpdateTextSize(String contStr) {
        // TODO Auto-generated method stub
        if(StringUtils.isNull(contStr)){
            return 32;
        }
        int length = ChannelContentUtil.getWordCount(contStr);
        if(length <= 13){
            return 32;
        }else if(length <= 19){
            return 23;
        }
        
        return 17;
    }
    /* SMARTISAN-280 zhaoxiachao 20161202 end */
    
    
    public static int getDrawaleFromNum(String imgStr){
        int bg = R.drawable.duoqu_v_hd_bg_blue;
        if(imgStr.equalsIgnoreCase("1050")){
            bg = R.drawable.duoqu_v_hd_bg_green;
        }else if(imgStr.equalsIgnoreCase("1021")){
            bg = R.drawable.duoqu_v_hd_bg_red;
        }else{
            bg = R.drawable.duoqu_v_hd_bg_blue;
        }
        return bg;
    }
    
    /* SMARTISAN-392 zhaoxiachao 20170119 start */
    public static String getBtnName(final JSONObject actionMap,String name, boolean isAddRemind) {
        if(!actionMap.optString("action").equalsIgnoreCase("SDK_TIME_REMIND")){
            isAddRemind = false;
        }
        // 默认：  简体中文
        String btnName = (String) JsonUtil.getValueFromJsonObject(actionMap, isAddRemind ? "egedName" : name);
        // 简体中文
        if ("zh-cn".equalsIgnoreCase(getSmartisanLanguage()) || "zh-tw".equalsIgnoreCase(getSmartisanLanguage())) {
            String zhName = (String) JsonUtil.getValueFromJsonObject(actionMap,  isAddRemind ? "edName" : "btn_name");
            if (!StringUtils.isNull(zhName)) {
                btnName = zhName;
            }
            return btnName;
        }
//         繁体中文
//        else if ("zh-tw".equalsIgnoreCase(getSmartisanLanguage())) {
//            String ftName = (String) JsonUtil.getValueFromJsonObject(actionMap, isAddRemind ? "ftedName" : "ftName");
//            if (!StringUtils.isNull(ftName)) {
//                btnName = ftName;
//            }
//            
//            return btnName;
//        }
//        // 韩语
//        if("ko".equalsIgnoreCase(getSmartisanLanguage())){
//            String ftName = (String) JsonUtil.getValueFromJsonObject(actionMap,  isAddRemind ? "kredName" : "krName");
//            if (!StringUtils.isNull(ftName)) {
//                btnName = ftName;
//            }
//        }else if("ja".equalsIgnoreCase(getSmartisanLanguage())){
//            //  日语
//            String ftName = (String) JsonUtil.getValueFromJsonObject(actionMap,  isAddRemind ? "jpedName" : "jpName");
//            if (!StringUtils.isNull(ftName)) {
//                btnName = ftName;
//            }
//        }else if("ru".equalsIgnoreCase(getSmartisanLanguage())){
//            // 俄语
//            String ftName = (String) JsonUtil.getValueFromJsonObject(actionMap,  isAddRemind ? "rsedName" : "rsName");
//            if (!StringUtils.isNull(ftName)) {
//                btnName = ftName;
//            }
//        }
        else{
            return btnName;
        }
    }
    
    public static String getSmartisanLanguage(){
        String language = Locale.getDefault().getLanguage();
        String country = Locale.getDefault().getCountry();
        if ("zh".equals(language)) {
            if ("HK".equalsIgnoreCase(country)
                    || "TW".equalsIgnoreCase(country)) {
                return "zh-tw";
            }
            return "zh-cn";
        } else {
            return language;
        }
    }
    
    /* SMARTISAN-392 zhaoxiachao 20170119 start */
    
}
